home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue53 / Clinic / CopyAFileForm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-12-01  |  1.5 KB  |  67 lines

  1. unit CopyAFileForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     btnCopy: TButton;
  12.     dlgSource: TOpenDialog;
  13.     dlgTarget: TSaveDialog;
  14.     procedure btnCopyClick(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.   public
  18.     { Public declarations }
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.DFM}
  27.  
  28. {$ifdef Ver90} //Delphi 2.0x doesn't have Win32Check
  29. type
  30.   EWin32Error = class(Exception);
  31.  
  32. procedure Win32Check(RetVal: Bool);
  33. begin
  34.   if not RetVal then
  35.     raise EWin32Error.Create(SysErrorMessage(GetLastError));
  36. end;
  37. {$endif}
  38.  
  39. procedure FileCopy(const Source, Target: String);
  40. var
  41.   LastError: DWord;
  42. begin
  43.   if not CopyFile(PChar(Source), PChar(Target), True) then
  44.   begin
  45.     LastError := GetLastError;
  46.     //If file exists, check it's okay to overwrite
  47.     if LastError = ERROR_FILE_EXISTS then
  48.     begin
  49.       if MessageDlg('Destination file exists. Overwrite?',
  50.            mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  51.         //If it's okay, then overwrite
  52.         Win32Check(CopyFile(PChar(Source), PChar(Target), False))
  53.     end
  54.     else
  55.       //If there was some other problem, report it
  56.       raise EWin32Error.Create(SysErrorMessage(LastError));
  57.   end
  58. end;
  59.  
  60. procedure TForm1.btnCopyClick(Sender: TObject);
  61. begin
  62.   if dlgSource.Execute and dlgTarget.Execute then
  63.     FileCopy(dlgSource.FileName, dlgTarget.FileName)
  64. end;
  65.  
  66. end.
  67.